<?php

/*	Created by Alexander Alexandrov aka SS

			hazg@mail.ru				*/
define('SSLIB_Text', TRUE);
define('SALT', 'whateveryouwant');

class Text
{
	static function MakeDottedNumber($num, $separator = ".", $step = 3)
	{
		$text = strrev($num);
		$text = chunk_split($text, $step, $separator);
		$text = trim($text, $separator);
		$text = strrev($text);
		return $text;
		//return strrev(trim(chunk_split(strrev($num), $step, $separator), $separator));
	}
	static function CutText($text, $len = 200, $ender = "...")
	{
		
		return mb_substr($text, 0, $len).((strlen($text) > ($len - 1))?$ender:"");
	}
	static function Lower($text)
	{
		return strtolower($text);
	}
	static function Convert($from, $to, $text)
	{
		return iconv($from,$to,$text);
	}
	
	static function Decrypt($text, $key, $iv = 'RFGBBB0f')
	{
		//	TODO:Crypt!
		$cipher = mcrypt_module_open(MCRYPT_BLOWFISH_COMPAT,'','cbc','');
		
		$text = strtr($text, '-_,', '+/=');
		$text = base64_decode($text);
		
		mcrypt_generic_init($cipher, $key, $iv);
		$decrypted = mdecrypt_generic($cipher,$text);
		mcrypt_generic_deinit($cipher);
		
		
		return $decrypted; 
		/*$bit_check=8;
		$cipher = mcrypt_module_open(MCRYPT_TRIPLEDES,'','cbc','');
		mcrypt_generic_init($cipher, $key, $iv);
		$decrypted = mdecrypt_generic($cipher,base64_decode($text));
		mcrypt_generic_deinit($cipher);
		$last_char=substr($decrypted,-1);
		for($i=0;$i<$bit_check-1; $i++)
		{
	        if(chr($i)==$last_char)
	        {        
	            $decrypted=substr($decrypted,0,strlen($decrypted)-$i);
	            break;
	        }
	    }
	    return $decrypted;*/
		
	}
	static function Crypt($text, $key, $iv = 'RFGBBB0f')
	{
		//TODO:Crypt!
		
		$cipher = mcrypt_module_open(MCRYPT_BLOWFISH_COMPAT,'','cbc','');
		
		mcrypt_generic_init($cipher, $key, $iv);
		$encrypted = mcrypt_generic($cipher,$text);
		mcrypt_generic_deinit($cipher);
		return strtr(base64_encode($encrypted), '+/=', '-_,');
		/*$bit_check=8;
		$text_num =str_split($text,$bit_check);
		$text_num = $bit_check-strlen($text_num[count($text_num)-1]);
		for ($i=0;$i<$text_num; $i++) {$text = $text . chr($text_num);}
		$cipher = mcrypt_module_open(MCRYPT_TRIPLEDES,'','cbc','');
		mcrypt_generic_init($cipher, $key, $iv);
		$decrypted = mcrypt_generic($cipher,$text);
		mcrypt_generic_deinit($cipher);
		return base64_encode($decrypted);*/		
	}
	static function Safe($text, $trimActiveHtml = false)
	{
	    if($trimActiveHtml)
	        $text = preg_replace('/<.*?(\w+)(.*?)>(.*?)<.*?>/i','$3', $text);
	    return str_replace(array('<?', '<?php', '?>'), '', $text);
	}
	function HtmlToTxt($text){
		$search = array('@<script[^>]*?>.*?</script>@si',  // Strip out javascript
		               '@<style[^>]*?>.*?</style>@siU',    // Strip style tags properly
		               '@<[\/\!]*?[^<>]*?>@si',            // Strip out HTML tags
		               '@<![\s\S]*?--[ \t\n\r]*>@'         // Strip multi-line comments including CDATA
		);
		return preg_replace($search, '', $text);
    }
     
	static function Tidy($text, $configPath)
	{
	    $ret  ='';
	    $tempPath = Sslib::SiteRoot('/tmp/').RandomKey();
	    
	    if(function_exists('tidy_parse_string'))
	    {
            file_put_contents($tempPath, $text);
		   
            $tidy = new tidy($tempPath, $configPath);
	        $tidy->CleanRepair();
			
	        unlink($tempPath);
            $ret = $tidy;
            
            
            unset($tidy);
            
	    }
	    else 
	    {
		    /*$tidyPath = Sslib::SiteRoot().'private/utils/tidy';
		    $cfgPath = Sslib::SiteRoot().'private/utils/configs/blogs';
		    if(!file_exists($tidyPath))
		        new Err(__T('file not found').' '.$tidyPath);
		    file_put_contents($tempPath, $text);
		    exec("$tidyPath -config $cfgPath \"$tempPath\"", $i, $ret);
	        $content = implode("\n",$i);
	        unlink($tempPath);
	        $ret = $content;*/
	    	new Err('нет модуля tidy. (отключение внешнего tidy для совместимости)');
	    }
        /*if(preg_match('#<body.*?>(.*?)</body>#is', $ret, $matchesBody))
        {
            if(preg_match_all('#<style.*?>(.*?)</style>#is', $ret, $matchesStyle, PREG_PATTERN_ORDER))
            {
                $styles = implode($matchesStyle[1]);
                $ret = '<style type="text/css">'.$styles.'</style>'.$matchesBody[1];
            }
            else $ret = $matchesBody[1];
        }*/
	    
	    return $ret;
	}
}

?>